package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.Constast;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.TreeNode;
import com.xjh.t.domain.Dept;
import com.xjh.t.service.IDeptService;
import com.xjh.t.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    //加载部门管理数
    @RequestMapping("loadDeptManagerLeftJson")
    public DataGridView loadDeptManagerLeftJson(DeptVo vo)
    {
        List<Dept> list = this.deptService.list();
        List<TreeNode> treeNodes = new ArrayList<>();
        for(Dept dept:list)
        {
            treeNodes.add(new TreeNode(dept.getId(),dept.getPid(),dept.getTitle(),dept.getOpen() == Constast.OPEN_TRUE));
        }

        return new DataGridView(treeNodes);
    }

    @RequestMapping("loadAllDept")
    public DataGridView loadAllDept(DeptVo vo)
    {
        IPage<Dept> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(vo.getAddress()),"address",vo.getAddress());
        queryWrapper.like(vo.getRemark() != null,"remark",vo.getRemark());
        queryWrapper.eq(vo.getId() != null,"id",vo.getId()).or().eq(vo.getId() != null,"pid",vo.getId());
        queryWrapper.orderByAsc("ordernum");
        this.deptService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addDept")
    public ResultObj addDept(DeptVo vo)
    {
        try
        {
            vo.setCreatetime(new Date());
            this.deptService.save(vo);
            return ResultObj.ADD_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum()
    {
        Map<String,Object> map = new HashMap<String,Object>();
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Dept> list = this.deptService.list(queryWrapper);
        if(list.size() > 0)
        {
            map.put("value",list.get(0).getOrdernum() + 1);
        }
        else
            map.put("value",1);
        return map;
    }

    @RequestMapping("updateDept")
    public ResultObj updateDept(DeptVo vo)
    {
        try
        {
            this.deptService.updateById(vo);
            return ResultObj.UPDATE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.UPDATE_ERROR;
        }

    }
    @RequestMapping("checkDeptHasChildrenNode")
    public Map<String,Object> checkDeptHasChildrenNode(DeptVo vo)
    {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(vo.getId() != null,"pid",vo.getId());
        List<Dept> list = this.deptService.list(queryWrapper);
        if(list.size() > 0)
        {
            map.put("value",true);
        }
        else
            map.put("value",false);
       return map;
    }

    @RequestMapping("deleteDept")
    public ResultObj deleteDept(Integer id)
    {
        try
        {
            this.deptService.removeById(id);
            return ResultObj.DELETE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }

    }
}

